part of quadcopterclient;

class CopterManagerManager {
  Map<String,QuadCopter> coptersById = new Map();
  Map<String,Element> elementsByCopterId = new Map();
  
  // Update the Copters and return a list of Elements to delete. 
  List<Element> updateCopters(QuadCopterManager latest) {
    Map<String,QuadCopter> newCopters = latest.getCopterMap();  
    
    List<Element> zToDelete;
    if (coptersById.isEmpty) {
      zToDelete = elementsByCopterId.values.toList();
      elementsByCopterId.clear();
    } else {
      newCopters.keys.forEach(_removeCopterById);
      zToDelete = new List();
      coptersById.keys.forEach((String id) {
        Element e = elementsByCopterId.remove(id);
        if (e != null) {
          zToDelete.add(e);
        }
      });
    }
    coptersById.addAll(newCopters);
    return zToDelete;
  }
  
  List<QuadCopter> getCopters() {
    return coptersById.values.toList();
  }
  
  Element getElementForCopter(QuadCopter pQuadCopter) {
    return elementsByCopterId[pQuadCopter.id]; 
  }
  
  void setElementForCopter(QuadCopter pQuadCopter, Element pElement) {
    elementsByCopterId[pQuadCopter.id] = pElement;  
  }
  
  void _removeCopterById(String pID) {
    coptersById.remove(pID);
  }
}